<template>
  <div class="page-container">
    <SearchForm
      :search-list="searchList"
      :search-params="searchParams"
      @search="onSearch"
      @reset="onReset"
    />
    <el-form>
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        height="100%"
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label" >
          <template slot-scope="scope">
            <el-image v-if="item.type === 'image'" :preview-src-list="(scope.row[item.prop] || '').split(',')" :src="scope.row[item.prop]" style="width: 30px;height: 30px;border: 1px solid #3CAA49" fit="contain" />
            <span v-else-if="item.type === 'select'">
              {{ item.mapping[scope.row[item.prop]] }}
            </span>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="150px">
          <template slot-scope="scope">
            <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
            <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http } from '@/api'
import Add from './add.vue'
export default {
  name: 'Course',
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.course,
      searchList: [
        {
          label: '课程分类',
          key: 'category_id',
          name: 'select',
          options: []
        },
        {
          label: '课程名称',
          key: 'name',
          name: 'input'
        }
      ],
      searchParams: {
        category_id: '',
        name: ''
      },
      columns: [
        { prop: 'categoryName', label: '课程分类' },
        { prop: 'name', label: '课程名称' },
        { prop: 'sort', label: '排序' },
        { prop: 'post_url', label: '图片', type: 'image' },
        { prop: 'created_time', label: '创建时间' }
      ],
      tableData: [
      ]
    }
  },
  created() {
    http.category.list().then(res => {
      this.searchList[0].options = res.data.map(el => ({ name: el.name, value: el.id }))
    })
  },
  methods: {
    formatTableData(data) {
      data.forEach(el => {
        if (el.CourseCategory) {
          el.categoryName = el.CourseCategory.name
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">
</style>
